package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.SaUserCheckPermission;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.*;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserRemindCommand;
import com.cskaoyan.wordmemorize.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@Tag(name = "移动端-用户(我的)模块")
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    UserService userService;

    @Operation(summary = "***获取用户信息",
            description = "在用户打开APP或者其他路由跳转的时候，通过token获取用户信息",
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            description = "成功. 返回登录成功的用户信息，以及当前等级权限码集合"
                    ),
                    @ApiResponse(responseCode = "500",
                            description = "请求失败"
                    )
            }
    )
    @GetMapping("info")
    public Result<UserDTO> getUserInfo() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserDTO userInfo = userService.getUserInfo(userId);
        return Result.ok(userInfo);
    }

    //    ------------------------ 叶佳杰部分：-----------------------------------
    //    APP端获取用户带升级信息:getUserNextDTO
    @GetMapping("upgrade/info")
    public Result<NextLevelDTO> getUserNextInfo(Long userId) {
        try {
            Long userIdInfo = StpKit.USER.getLoginIdAsLong();
            NextLevelDTO userNextDTO = userService.getUserNextDTO(userIdInfo);
            return Result.ok(userNextDTO);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //    APP端获取用户等级信息：getUserLevelInfo
    @GetMapping("/level/info")
    public Result<DisplayUserLevelDTO> getUserLevelInfoContent(Long userId) {
        try {
            Long userIdInfo = StpKit.USER.getLoginIdAsLong();

            DisplayUserLevelDTO userLevelInfo = userService.getUserLevelInfo(userIdInfo);
            return Result.ok(userLevelInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //    APP端更新用户每天计划学习的单词量:updateVocCountOfDay
    @PutMapping("/day/voc")
    public Result<String> updateVocCountOfDayContent(Integer count) {
        if (count > 20) {
            Long userIdInfo = StpKit.USER.getLoginIdAsLong();

            userService.updateVocCountOfDay(userIdInfo, count);
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //    APP端更新用户的学习提醒时间:updateUserRemind
    @PutMapping("/remind")
    public Result<String> updateUserRemindInfo(@RequestBody UserRemindCommand command) {
        if (command != null) {
            userService.updateUserRemind(command);
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //    APP端获取用户的单词上限统计信息:getUserVocBoundStatistics
    @GetMapping("/voc/bound/statistics")
    public Result<UserVocBoundStatisticsDTO> getUserVocBoundStatisticsInfo(Long userId) {
        Long userIdInfo = StpKit.USER.getLoginIdAsLong();

        if (userIdInfo != null) {

            UserVocBoundStatisticsDTO userVocBoundStatistics = userService.getUserVocBoundStatistics(userIdInfo);
            return Result.ok(userVocBoundStatistics);
        } else {
            return Result.fail();
        }
    }

    //    APP端获取用户我的页面展示的信息:getUserPage
    @GetMapping("/voc/my/page")
    public Result<UserPageDTO> getUserPageInfo(Long userId) {
        Long userIdInfo = StpKit.USER.getLoginIdAsLong();
        if (userIdInfo != null) {

            UserPageDTO userPage = userService.getUserPage(userIdInfo);
            return Result.ok(userPage);
        } else {
            return Result.fail();
        }
    }

}
